/**
 * Created by IAN on 2016/10/14.
 */
$(function () {
    //加载角色信息
    var selRole = $.Params.create();
    selRole.ajax(getIP()+"powerRole/getPowerRoles",function(data){
        var html = "<option value='0'>请选择角色</option>";
        $.each(data.result,function(i,val){
            html+="<option value='"+val.id+"'>"+val.roleName+"</option>";
        })
        $("#powerRoleId").html(html);
    })

    //查询用户信息
    var eid = GetQueryString("eid");
    var selUser = $.Params.create();
    selUser.addParams("id",eid);
    selUser.ajax(getIP()+"users/getPowerUsers",function(data){
        if(data.success){
            var val = data.result[0];
            $("#loginName").val(val.loginName)
            $("#userName").val(val.userName);
            var tel = val.tel;
            var email = val.email;
            if(tel == null){
                tel="";
            }
            if(email == null){
                email = "";
            }
            $("#tel").val(tel);
            $("#email").val(email)
            $("#powerRoleId").val(val.powerRoleId)

        }
    })



    //添加信息
    $("#eid").val(eid);
    $("#updateTime").val(getNowDateTime());
    $("#updateUserName").val("fred");
    $("#addData").on("click",function(){
        var loginName = $("#loginName").val().trim();
        var userName = $("#userName").val().trim();
        var tel = $("#tel").val().trim();
        var email = $("#email").val().trim();
        var powerRoleId = $("#powerRoleId").val();
        if(loginName ==""){
            alert("请输入用户名！");
            return false;
        }
        if(userName ==""){
            alert("请输入姓名！");
            return false;
        }
        if(tel != ""){
            if(!valPhone(tel)){
                alert("请输入正确的电话号码！");
                return false;
            }
        }
        if(email != ""){
            if(!checkEmail(email)){
                alert("请输入正确格式的邮箱！");
                return false;
            }
        }
        if(powerRoleId =="0"){
            alert("请选择角色！");
            return false;
        }
        $("#addForm").attr("action",getIP()+"users/updatePowerUser");
        $("#addForm").submit();
    })


    //取消修改信息
    $("#cancelAdd").on("click",function() {
        history.back(-1);
    })


});